﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using IronSuit.DataMap;
using IronSuit.Crazy250API.UserAPI;
using IronSuit.Crazy250API.BaseLogic;

namespace IronSuit.Crazy250API.LoginAPI
{
	public class LoginHelper
	{
		#region 变量定义
		readonly static string createUserSql = @"CREATE TABLE crazy_user{0}.user_{1} (
    `Category` varchar(128) NOT NULL,
  `Key` varchar(128) NOT NULL,
  `Value` varchar(512) NOT NULL,
  `Efield1` varchar(128) DEFAULT NULL,
  `Efield2` varchar(128) DEFAULT NULL,
  `Efield3` varchar(128) DEFAULT NULL,
  `Efield4` varchar(128) DEFAULT NULL,
  `Efield5` varchar(128) DEFAULT NULL,
  `Efield6` varchar(128) DEFAULT NULL,
  `Efield7` varchar(128) DEFAULT NULL,
  `Efield8` varchar(128) DEFAULT NULL,
  `Efield9` varchar(128) DEFAULT NULL,
  `Modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY `CategoryKey` (`Category`,`Key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
";
		#endregion
		public static string activeUserDB
		{
			get
			{
				return "1";
			}
		}
		static public List<APIItem> CheckLogin(Dictionary<string, string> inputDic)
		{
			List<APIItem> outputDic = new List<APIItem>();
			string SessionId = inputDic["SessionId"];
			string UserId = inputDic["UserId"];
			string[] userArray = UserId.Split('_');
			string DBId = userArray[0];
			UserId = userArray[1];
			using (DBFactory dbf = new DBFactory("PeopleConn"))
			{
				CrazyPeople p = new CrazyPeople(int.Parse(UserId));
				if (dbf.Query(p))
				{
					if (p.UserDB != DBId)
						throw CrazyException.CE006ParameterError;
					UserQueryer uq = UserQueryer.GetQueryer(p);
					string dbSessionId = uq.QueryDataValue(SmallType.Login, "SessionId");
					if(dbSessionId !=SessionId)
						throw CrazyException.CE006ParameterError;
				}
				else
					throw CrazyException.CE003EMailNotExist;
			}
			return outputDic;
		}
		static public void Register(Dictionary<string, string> inputDic)
		{
			string Email = inputDic["Email"];
			string Password = inputDic["Password"];
			string NickName = inputDic["NickName"];
			using (DBFactory dbf = new DBFactory("PeopleConn"))
			{
				CrazyPeople p = new CrazyPeople();
				p.EMail = Email;
				long ec = dbf.Count(p);
				if (ec > 0)
					throw CrazyException.CE001SameEamil;
				p.Clear();
				p.NickName = NickName;
				ec = dbf.Count(p);
				if (ec > 0)
					throw CrazyException.CE002SameNickName;
				p.EMail = Email;
				p.Password = Password;
				p.UserDB = activeUserDB;
				if (dbf.Insert(p))
				{
					p.Clear();
					p.EMail = Email;
					List<DataEntity> dList = dbf.QueryList(p);
					p.Displace(dList[0]);
					using (DBFactory userDBF = new DBFactory("UserConn"))
					{
						userDBF.ExecSql(string.Format(createUserSql, p.UserDB, p.PeopleID.ToString()));
					}
				}
			}
		}
		static public List<APIItem> Login(Dictionary<string, string> inputDic)
		{
			List<APIItem> outputDic = new List<APIItem>();
			string Email = inputDic["Email"];
			string Password = inputDic["Password"];
			using (DBFactory dbf = new DBFactory("PeopleConn"))
			{
				CrazyPeople p = new CrazyPeople();
				p.EMail = Email;
				if (dbf.QueryByField(p))
				{
					if (p.Password != Password)
						throw CrazyException.CE004WorongPassword;
					UserHandler uh = UserHandler.GetHandler(p);
					Guid g = Guid.NewGuid();					
					uh.UpdateData(SmallType.Login, "SessionId", g.ToString());
					outputDic.Add(new APIItem("SessionId", g));
					outputDic.Add(new APIItem("UserId", p.UserDB.ToString() + "_" + p.PeopleID));
					outputDic.Add(new APIItem("NickName", p.NickName));
				}
				else
					throw CrazyException.CE003EMailNotExist;

			}
			return outputDic;
		}
		
	}
}
